package com.dingding.dao.usermangerdao.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.entity.Vehicleinfo;
import com.dingding.dao.basedao.impl.BaseDaoImpl;
import com.dingding.dao.sql.VehicleInfoDaoSql;
import com.dingding.dao.usermangerdao.IVehicleinfoDao;

public class VehicleinfoDaoImpl extends BaseDaoImpl<Vehicleinfo> implements
		IVehicleinfoDao {

	private static final Logger log = Logger
			.getLogger(VehicleinfoDaoImpl.class);

	/**
	 * @Title: getVehicleinfo
	 * @Description:根据用户ID获取用户车辆信息
	 * @author cheshaowei
	 * @throws
	 */
	@Override
	public Vehicleinfo getVehicleinfo(String userId) {

		// 根据用户ID获取用户车辆信息
		Vehicleinfo vehicleinfo = null;
		try {
			Criterion lisencePlate = Restrictions.eq("loginId", userId);
			vehicleinfo = super.queryCriteriaUnResult(lisencePlate, false);
		} catch (Exception e) {
			log.error("根据用户ID获取用户车辆信息出现异常！", e);
		}

		return vehicleinfo;

	}

	/**
	 * 
	 * @Title: getVehicleinfo
	 * @Description:根据车牌获取车辆主键ID,用来判断当前车牌是否已经注册过
	 * @param lisencePlate
	 *            待注册的车牌号
	 * @author cheshaowei
	 * @throws
	 * 
	 */
	@Override
	public String getUvId(String lisencePlate,String userId) {

		String uvId = null;
		try {
			
			Object[] obj = super.sqlQueryUnResultObj(
					VehicleInfoDaoSql.GET_VEHICLE_INFO_ID, false, lisencePlate, userId);
			if (obj != null) {
				uvId = obj[0].toString();
			}
			
		} catch (Exception e) {
			log.error("根据车牌获取车辆主键ID出现异常！", e);
			return null;
		}

		return uvId;
	}

	/**
	 * 初始化车辆认证信息
	 */
	@Override
	public void saveVehicleInfo(String viid, String loginId) {

		Vehicleinfo vehicleinfo = new Vehicleinfo();
		//viid
		vehicleinfo.setUvId(viid);
		//loginId
		vehicleinfo.setLoginId(loginId);
		//认证时间
		vehicleinfo.setCreateTime(new Date());
		//认证状态 0:保留
		vehicleinfo.setVstatus(0);
		//是否认证
		vehicleinfo.setIsAuth(1);
		
		super.save(vehicleinfo);
	}
	
	/**
	 * 初始化车辆认证信息
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveVehicleInfoByNum(String viid, String loginId) {

		Vehicleinfo vehicleinfo = new Vehicleinfo();
		//viid
		vehicleinfo.setUvId(viid);
		//loginId
		vehicleinfo.setLoginId(loginId);
		//认证时间
		vehicleinfo.setCreateTime(new Date());
		//认证状态 0:保留
		vehicleinfo.setVstatus(0);
		//是否认证
		vehicleinfo.setIsAuth(1);
		
		super.save(vehicleinfo);
	}
	
	
}
